﻿using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using Autofac;
using DDCMS.Core;
using Autofac.Extensions.DependencyInjection;
using DDCMS.Core.Dependency;
using DDCMS.Domain.Manager;
using DDCMS.EntityFrameworkCore;
using NLog.Extensions.Logging;

namespace DDCMS
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }
        public IContainer ApplicationContainer { get; private set; }
        public IConfigurationRoot Configuration { get; }
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
           
            services.AddMvc();
            //services.AddDbContext<CMSContext>(x=>x.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
            services.AddDbContext<CMSContext>(x=>x.UseMySql(Configuration.GetConnectionString("DefaultConnection"),b=>b.MigrationsAssembly("DDCMS")));
            services.AddIdentity<User, Role>(options =>
            {
                options.Cookies.ApplicationCookie.AuthenticationScheme = "DDApplicationCookie";
                options.Cookies.ApplicationCookie.CookieName = "DDCMS";
            })
            .AddEntityFrameworkStores<CMSContext>()
            .AddDefaultTokenProviders();
            var builder = new ContainerBuilder();
            builder.RegisterModule<EFModule>();
            builder.RegisterModule<CoreModule>();
            Type baseType = typeof(IDependency);
            builder.Populate(services);
            this.ApplicationContainer = builder.Build();
            return new AutofacServiceProvider(this.ApplicationContainer);
        }


        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
        {
            loggerFactory.AddNLog();
            string con = Configuration.GetConnectionString("ConnectionStrings");
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage(); 
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();
            app.UseIdentity();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Login}/{action=Index}/{id?}");
            });
            appLifetime.ApplicationStopped.Register(() => this.ApplicationContainer.Dispose());
        }
       

    }
}
